package com.xdqzy.baseline.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    private DateUtils() {

    }

    public static final String FULL_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String FULL_TIME_FORMAT1 = "yyyyMMddHHmmss";

    public static final String MONTH_FORMAT = "yyyy-MM";

    public static final String DAY_FORMAT = "yyyyMMdd";

    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static final String CHINE_DATE_FORMAT="yyyy年MM月dd日";

    public static final String CHINE_DAY_DATE_FORMAT="MM月dd日";
    /**
     * 精确到毫秒的时间戳
     */
    public static final String MILL_SECOND_FORMAT="yyyyMMddHHmmssSSS";

    public static String getNowTime() {
        return DateTime.now().toString(FULL_TIME_FORMAT);
    }

    /**
     * yyyyMMddHHmmss
     *
     * @return
     */
    public static String getNowShortTime() {
        return DateTime.now().toString(FULL_TIME_FORMAT1);
    }


    /**
     * 获取月份的开始时间和结束和结束时间
     *
     * @param month yyyy-MM
     * @return 时间数组，开始和结束时间
     */
    public static Date[] getMonthStarAndEnd(String month) {
        DateTime dateTime = DateTime.parse(month, DateTimeFormat.forPattern(MONTH_FORMAT));
        Date start = dateTime.dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue().toDate();
        Date end = dateTime.dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue().toDate();
        return new Date[]{start, end};
    }

    public static Date formatStringDate(String time) throws ParseException {
        return StringUtils.isNoneBlank(time) ? new SimpleDateFormat(FULL_TIME_FORMAT1).parse(time) : null;
    }

    /**
     * 按照 yyyy-MM-dd HH:mm:ss 转换日期
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static Date formatFullStringDate(String time) throws ParseException {
        return StringUtils.isNoneBlank(time) ? new SimpleDateFormat(FULL_TIME_FORMAT).parse(time) : null;
    }

    public static Date getNextWeekday(int weekday) {
        DateTime now = DateTime.now();
        if (now.getDayOfWeek() > weekday) {
            now = now.plusDays(7);
        }
        return now.withDayOfWeek(weekday).toDate();
    }

    public static Date getCurWeekday(int weekday) {
        DateTime now = DateTime.now();
        return now.withDayOfWeek(weekday).toDate();
    }

    public static String formatDateToString(Date date) {
        if (date == null) {
            return null;
        }
        return new DateTime(date).toString(FULL_TIME_FORMAT);
    }


    public static String convertFormat(String date,String sourceFormat,String targetFormat){
        return DateTime.parse(date, DateTimeFormat.forPattern(sourceFormat)).toString(targetFormat);
    }

    public static String getCurTime(String format) {
        return new SimpleDateFormat(format).format(new Date());
    }

    public static String getTodayStart(){
        return DateTime.now().millisOfDay().withMinimumValue().toString(FULL_TIME_FORMAT);
    }

    public static String getTodayEnd(){
        return DateTime.now().millisOfDay().withMaximumValue().toString(FULL_TIME_FORMAT);
    }

    public static int getCurWeekday(){
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        return c.get(Calendar.DAY_OF_WEEK)-1;
    }

    public static boolean compareTime(int hour,int minute){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        Date time=calendar.getTime();
        return time.after(new Date());
    }

    public static String getMillTimeStr(){
        return DateTime.now().toString(MILL_SECOND_FORMAT);
    }

    public static Date parseStrToDate(String date,String time){
        return DateTime.parse(date+" "+time,DateTimeFormat.forPattern(FULL_TIME_FORMAT)).toDate();
    }
}
